/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
 * PTDVariableTable.java
 *
 * Created on March 10, 2006, 8:36 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package ptd;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.*;
import javax.swing.event.TableModelListener;
import javax.swing.event.TableModelEvent;
import java.util.Vector;
import javax.swing.JTextField;
import javax.swing.tree.TreeCellRenderer;


/**
 *
 * @author John
 */
public class PTDVariableTable extends JTable{
    
    public VarTableModel MyModel;
    
    /** Creates a new instance of PTDVariableTable */
    public PTDVariableTable() {
        MyModel = new VarTableModel();
        MyModel.init();
        MyModel.addTableModelListener(new VarTableModelListener());
        setModel(MyModel);
        
        //setDefaultRenderer(VarTableRenderer.class, new VarTableRenderer());
        int vColIndex = 1;
        TableColumn col = getColumnModel().getColumn(vColIndex);
        col.setCellRenderer(new VarTableRenderer());
        
    }
    
    public boolean isCellEditable(int rowIndex, int colIndex) {
        //if(colIndex > 0){
        //    return true;   // actually, want to keep this because want user to
        //                   // be able to copy the data
        //}else{
            return true;
        //}
    }
    
    //  Table Model Listener -----------------------------------------
    public class VarTableModelListener implements TableModelListener{
        public void tableChanged(TableModelEvent e) {
            int row = e.getFirstRow();     // This only detects the first row that changed
            int column = e.getColumn();
            VarTableModel model = (VarTableModel)e.getSource();
                        
            // Only interested in first column changes
            if(column == 0){
                model.setUpdateState(1);
            }else{
            }
            
            //model.fi
            // Check out fireRowsInserted in AbstractTableModel
        }
    }
    
    //  Table Renderer -----------------------------------------
    public class VarTableRenderer extends JTextField implements TableCellRenderer{
        
        VarTableRenderer(){
            setEditable(false);
            setBorder(null);
            //setForeground(Color.WHITE);
            this.setBackground(Color.WHITE);
        }
        
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            setText((String)value);
            return this;
        }
        
        //public void validate() {}
        //public void revalidate() {}
        //protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}
        //public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
    
    }
    
    //  Table Model -----------------------------------------
    public class VarTableModel extends AbstractTableModel{
    //public class VarTableModel extends javax.swing.table.DefaultTableModel{
        
        String[] columnNames = {"Variable Name",
                                "Value"};
                     
        Vector<String> names;                        
        Vector<String> values;
                                        
        private int m_UpdateState = 0;

        public void init(){
            names = new Vector<String>();
            values = new Vector<String>();
            names.add("");
            names.add("");
            names.add("");
            values.add("");
            values.add("");
            values.add("");
        }
        
        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return names.size();
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            if(col == 0){
                return names.elementAt(row);
            }else{
                return values.elementAt(row);
            }
        }

        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col == 0) {
                return true;
            }
            
            return true;
        }
        
        /*
        * Don't need to implement this method unless your table's
        * data can change.
        */
        // This has to be Object, else it wont be an override!
        public void setValueAt(Object value, int row, int col) {
            if(col == 0){
                names.setElementAt((String)value, row);
            }else{
                values.setElementAt((String)value, row);
            }
            
            if(row == (names.size() - 1)){  // If last row being edited, add new rows
                names.add("");
                values.add("");
            }
            
            cleanTable();
            
            fireTableCellUpdated(row, col);
        }
        
        public void cleanTable(){
            int sz = names.size();
            int ctr = 0;
            String s;
            for(ctr = 0; ctr < sz-1; ctr++){
                s = (String)names.get(ctr);
                if(s != null){
                    if(s.length() <= 0){
                        names.remove(ctr);
                        values.remove(ctr);
                        this.fireTableRowsDeleted(ctr, ctr);
                        sz = names.size();
                        ctr--;  // Want to check the row again
                    }
                }
            }
            
        }

        public String getVariableName(int Index){
            if(Index > names.size()){
                return null;
            }
            String Ret = (String)names.get(Index);
            if(Ret.length() <= 0){
                return null;
            }
            return Ret;
        }
        
        public int setVariableResult(int Index, String Result){
            if(Index > names.size()){
                return 0;
            }
            
            values.setElementAt(Result, Index);
            this.fireTableCellUpdated(Index, 1);
            return 1;
        }
        
        public void setUpdateState(int newState){
            m_UpdateState = newState;
        }
        
        public int getUpdateState(){
            int Ret = m_UpdateState;
            m_UpdateState = 0;
            return Ret;
        }
    }
}
